package com.itcast.test;

import com.itcast.dao.CutomerDao.CustomerDao;
import com.itcast.domain.Customer.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.List;

//声明Spring中提供的单元测试
@RunWith(SpringJUnit4ClassRunner.class)
//加载配置文件信息
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class CustomerTset {
    @Autowired
    public CustomerDao customerDao;

    /**
     * findOne：根据id查询信息，返回对象
     */
    @Test
    public void findOne() {
        Customer customer = customerDao.findOne(1L);
        System.out.println(customer);

    }

    /**
     * findAll:查询全部信息
     */

    @Test
    public void findAll() {
        List<Customer> list = customerDao.findAll();
        for (Customer c : list) {
            System.out.println(c);
        }
    }

    /**
     * save:保存和修改
     * 根据是否有ID去判断
     */
    @Test
    public void save() {
        Customer customer = new Customer();
        customer.setCustName("清华");
        customer.setCustLevel("VIP");
        customer.setCustIndustry("导弹");
        customerDao.save(customer);
    }

    /**
     * save：修改信息
     */
    @Test
    public void update() {
        Customer customer = new Customer();
        customer.setCustId(5L);
        customer.setCustName("西北");
        customer.setCustLevel("VVIP");
        customer.setCustIndustry("飞机");
        customerDao.save(customer);
    }

    /**
     * delete:根据ID删除信息
     */
    @Test
    public void delete() {
        customerDao.delete(3L);
    }

    /**
     * 统计查询 一共有的数据是多少条
     */
    @Test
    public void count() {
        long count = customerDao.count();
        System.out.println("一共有的数据条数是"+count);
    }
    /**
     * 判断某一个id的完整信息是否存在
     */
    @Test
    public void exist(){
        boolean exists = customerDao.exists(2L);
        System.out.println("id为4的信息是否存在:"+  exists);
    }
    /**
     * 根据id从数据库中查询
     * @Transient保证getOne的正常运行
     * findOne:
     * em.find();    立即加载
     * getOne:
     * em.getReference   延时加载
     * 返回的是一个客户的动态代理对象
     * 什么时候用，什么时候加载
     *
     */
    @Test
    @Transactional
    public void  testGetOne() {
        Customer customer = customerDao.getOne(4l);
        System.out.println(customer);
    }
}